//彩铃接口
importClass(Packages.org.framework2.utils.XMLReader);
importClass(Packages.org.framework2.utils.HttpClientUtil);
importClass(Packages.org.framework2.utils.DBTools);
importClass(Packages.org.framework2.utils.LogUtils);
importClass(Packages.org.framework2.utils.StringUtil);
LogUtils.initLogID();
var logTitle = '彩铃平台测试桩接口:';
/**
 * --------------------------------------------
 * step-01:获取当前测试的用例的id
 * --------------------------------------------
 */
var sqlCaseID = 'select caseid from cms_test_mock_caseid t where rownum = 1';
var resutSetOfCaseID = dbTools.select(sqlCaseID);
var caseid = resutSetOfCaseID.get(0).get('caseid');
LogUtils.info(logTitle,caseid);
LogUtils.info(logTitle,'运行测试用例[ ' + caseid + ' ]');

/**
 * --------------------------------------------
 * step-02:获取请求报文
 * --------------------------------------------
 */
var xmlData = request.getParameter('xmldata');
LogUtils.info(logTitle,'收到请求报文[ ' + xmlData + ' ]');
/**
 * --------------------------------------------
 * step-03:将请求报文保存在CMS_TEST_MOCK_LOG表中
 * --------------------------------------------
 */
dbTools.insertClobXML(caseid, '1', xmlData);

if(null != xmlData && xmlData.trim().length()>0)
{
    //转换为xml报文
    //var xmlReader = new XMLReader(xmlData);
    
    /**
     * --------------------------------------------
     * step-04:获取响应信息
     * --------------------------------------------
     */
    var sqlforresponse = 'select * from (';
    sqlforresponse += ' ' + 'select * from cms_test_mock_response where caseid = ' + caseid + ' and type=\'0\' order by sequence'; 
    sqlforresponse += ' ' + ')where rownum = 1';
    LogUtils.info(logTitle,'查询响应信息的SQL为[ ' + sqlforresponse + ' ]');
    var responseMessage = dbTools.select(sqlforresponse);
    
    if(responseMessage != null && responseMessage.size() > 0)
    {
    	var responseIbusLog = responseMessage.get(0);
    	
    	/**
    	 * --------------------------------------------
         * step-05:响应CMS
         * --------------------------------------------
         */
    	var message = responseIbusLog.get('message');
    	if(null !=message )
    	{
    	   response.write(new java.lang.String(message).trim().replaceAll('\n',''));
    	   response.getOutputStream2().flush();
    	}
    	dbTools.insertClobXML(caseid, '2', message);
    	LogUtils.info(logTitle,'响应的信息为：[ ' + message + ' ]');
    	
    	/**
    	 * --------------------------------------------
         * step-06:修改数据库，该条数据已使用
         * --------------------------------------------
         */
    	var sequence = responseIbusLog.get('sequence');
    	var updateresponse = 'update cms_test_mock_response t set t.type=\'1\' where t.caseid='+ caseid + ' and t.sequence = ' + sequence;
    	dbTools.update(updateresponse);
    	LogUtils.info(logTitle,'更新响应信息[caseid:' + caseid + '; sequence:' + sequence +'] 为已使用状态...');
    }
    else
    {
    	LogUtils.info(logTitle,'没有找到相应的响应记录');
    }
    
    /**
     * --------------------------------------------
     * step-07:查询回执信息
     * --------------------------------------------
     */
    var sqlforreturn = 'select * from cms_test_mock_return t where t.caseid = '+ caseid +' and t.type = \'0\'';
    sqlforreturn += ' ' + 'and t.mgroup in (select min(t2.mgroup) from cms_test_mock_return t2 where t2.caseid= '+ caseid +' and t2.type = \'0\')'; 
    sqlforreturn += ' ' + 'order by t.sequence';
    LogUtils.info(logTitle,'查询回执信息的SQL[ ' + sqlforreturn + ' ]');
    var returnMessages = dbTools.queryForResultSet(sqlforreturn);
    if(returnMessages !=null)
    {
        while (returnMessages.next())
        {
            var message = dbTools.getClobMessage(returnMessages, 'message');
                    
            //回执地址
            var url = dbTools.getStringMessage(returnMessages, 'url');
            
            //回执延迟时间
            var delay = dbTools.getNumberMessage(returnMessages, 'delay') * 1000;
            
            /**
    	     * --------------------------------------------
             * step-08:发送回执
             * --------------------------------------------
             */
            var map = new java.util.HashMap();
            map.put('xmldata',new java.lang.String(message).replaceAll('\n',''));
            HttpClientUtil.post(url, delay, map);
            dbTools.insertClobXML(caseid, '3', map.get('xmldata'));
            LogUtils.info(logTitle,'发送回执：回执地址为[' + url + ']; 回执报文为[' + map.get('xmldata') + ']');
            
            /**
    	     * --------------------------------------------
             * step-09:修改数据库，该条数据已使用
             * --------------------------------------------
             */
            var mgroup = dbTools.getNumberMessage(returnMessages, 'mgroup');
            var sequence = dbTools.getNumberMessage(returnMessages, 'sequence');
            var updateReturn = 'update cms_test_mock_return t set t.type = \'1\' where t.caseid ='+ caseid +' and t.mgroup='+ mgroup +' and t.sequence = '+ sequence;
            dbTools.update(updateReturn);
            LogUtils.info(logTitle,'更新回执信息[caseid:' + caseid + '; mgroup:' + mgroup + '; sequence:' + sequence +'] 为已使用状态...');
        }
    }
    else
    {
        LogUtils.info(logTitle,'没有找到相应的回执记录');
    }
}